package com.study.clothes.utils;

import com.study.clothes.pojo.Goods;
import com.study.clothes.pojo.ImgBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ImgUtils {
    public static List<Goods> getImg(String keyWord, Integer page) throws IOException {
        List<Goods> list = new ArrayList<>();
        String url="https://search.jd.com/Search?keyword="+keyWord+"&page="+page;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");

        Elements a = element.getElementsByTag("a");
        for (Element element1 : elements) {
            String img=element1.getElementsByTag("img").eq(0).attr("data-lazy-img");

            String text=element1.getElementsByClass("p-price").eq(0).text();
            String title=element1.getElementsByClass("p-name").eq(0).text();
            if (text==null||text.equals("")){
                continue;
            }
            if (title==null||title.equals("")){
                continue;
            }
            if (img==null||img.equals("")){
                continue;
            }
            Goods goods = new Goods();
            goods.setImgUrl(img);
//            if (title.length()>=25){
//                title = title.substring(0, 26);
//            }
            String[] s = text.split("￥");
            goods.setName(title);
            goods.setPrice(s[1]);
            list.add(goods);
        }
        return list;


    }
}
